<template>
    <div :id="chartId" class="main"></div>
  </template>
  
  <script setup>
  import * as echarts from 'echarts';
  import { onMounted, ref, watch } from 'vue';
  
  const props = defineProps({
    chartType: { // 图表类型
      type: String,
      default: 'both',
      validator: (value) => ['bar', 'line', 'both'].includes(value),
    },
    barColor: { // 柱状图颜色
      type: String,
      default: '#5470c6',
    },
    lineColor: { // 折线图颜色
      type: String,
      default: '#91cc75',
    },
    legendFontSize: {//数据 属性名文字大小
      type: Number,
      default: 12,
    },
    tooltipFontSize: { //图表提示文本字体大小
      type: Number,
      default: 14,
    },
    xAxisLabelFontSize: { //X轴字体大小
      type: Number,
      default: 12,
    },
    yAxisLabelFontSize: {//Y轴字体大小
      type: Number,
      default: 12,
    },
    showTooltip: { // 是否显示图表提示文本
      type: Boolean,
      default: true,
    },
  });
  
  const chartId = ref('main-' + Math.random().toString(36).substr(2, 9));
  let myChart = null;
  
  onMounted(() => {
    initChart();
  });
  
  // 监听 props 变化，重新初始化图表
  watch(
    [
      () => props.chartType,
      () => props.barColor,
      () => props.lineColor,
      () => props.legendFontSize,
      () => props.tooltipFontSize,
      () => props.xAxisLabelFontSize,
      () => props.yAxisLabelFontSize,
      () => props.showTooltip, 
    ],
    () => {
      initChart();
    }
  );
  
  function initChart() {
    const chartDom = document.getElementById(chartId.value);
    if (!myChart) {
      myChart = echarts.init(chartDom);
    }
  
    const seriesDefinitions = [
      {
        name: 'Evaporation',
        data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
      },
      {
        name: 'Precipitation',
        data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
      },
      {
        name: 'Temperature',
        data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
      },
    ];
  
    let series = [];
    if (props.chartType === 'bar' || props.chartType === 'both') {
      seriesDefinitions.forEach((seriesDef) => {
        series.push({
          name: seriesDef.name,
          type: 'bar',
          data: seriesDef.data,
          itemStyle: { color: props.barColor },
          tooltip: {
            valueFormatter: function (value) {
              return value + ' ml';
            },
          },
          group: seriesDef.name,
        });
      });
    }
    if (props.chartType === 'line' || props.chartType === 'both') {
      seriesDefinitions.forEach((seriesDef) => {
        series.push({
          name: seriesDef.name,
          type: 'line',
          data: seriesDef.data,
          itemStyle: { color: props.lineColor },
          tooltip: {
            valueFormatter: function (value) {
              return value + ' ml';
            },
          },
          group: seriesDef.name,
        });
      });
    }
  
    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          crossStyle: {
            color: '#999',
          },
        },
        textStyle: {
          fontSize: props.tooltipFontSize,
        },
        show: props.showTooltip,
      },
      toolbox: {
        feature: {
          dataView: { show: true, readOnly: false },
          magicType: { show: true, type: ['line', 'bar'] },
          restore: { show: true },
          saveAsImage: { show: true },
        },
      },
      legend: {
        data: seriesDefinitions.map(s => s.name),
        selectedMode: 'group',
        textStyle: {
          fontSize: props.legendFontSize,
        },
      },
      xAxis: [
        {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon'],
          axisPointer: {
            type: 'shadow',
          },
          axisLabel: {
            interval: 1,
            fontSize: props.xAxisLabelFontSize,
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
          name: 'Value',
          min: 0,
          max: 250,
          interval: 50,
          axisLabel: {
            formatter: '{value} ml',
            fontSize: props.yAxisLabelFontSize,
          },
        },
      ],
      series: series,
    };
  
    myChart.setOption(option, true);
  }
  </script>
  
  <style scoped>
  .main {
    width: 1000px;
    height: 500px;
  }
  </style>